// NMI's Java Code Viewer 5.1  1997-2001 B. Lemaire
// http://njcv.htmlplanet.com - info@njcv.htmlplanet.com

// Copy registered to Evaluation Copy                                   

// Source File Name:   SoundObjectsApplet.java

import java.applet.Applet;
import java.awt.*;
import java.io.PrintStream;
import sun.audio.*;
import symantec.itools.awt.Slider;
import symantec.itools.awt.VerticalSlider;

public class SoundObjectsApplet extends Applet
    implements Runnable {

    private protected static final double SAMPLING_RATE = 8012D;
    private protected static final int STRING = 0;
    private protected static final int CPLATEC = 1;
    private protected static final int BARCC = 2;
    private protected static final int BARCF = 3;
    private protected static final int CMEMBRANE = 4;
    private protected static final int PSEUDOSTRING = 5;
    private protected static final int FILESOB = 6;
    private protected static final int BANG = 0;
    private protected static final int SCRAPE = 1;
    private protected static final int PLUCK = 2;
    private protected static final int BANG2 = 3;
    private protected static final int WHITENOISE = 4;
    private protected static final int GAUSSIANNOISE = 5;
    private protected static final double MAXMAT = 10000D;
    private protected static final double MINMAT = 1D;
    private protected static final double MAXFREQ = 1000D;
    private protected static final double MINFREQ = 1D;
    private protected static final int MINGRIDPOINTS = 1;
    private protected static final int MAXGRIDPOINTS = 10000;
    private protected static final double MAXSAMPLETIME = 60D;
    private protected static final double MINSAMPLETIME = 0.0001D;
    private protected static final double MINPLUCKTIME = 0.0001D;
    private protected static final double MINSCRAPEDUR = 0.01D;
    private protected static final double MAXSCRAPEDUR = 1D;
    private protected static final double MINMALLETHARDNESS = 10D;
    private protected static final double MAXMALLETHARDNESS = 100000D;
    private protected static final double MIN_SHORT_VOL = 500D;
    private protected static final double MAX_SHORT_VOL = 20000D;
    private int n_sobjects;
    private int n_samples;
    private int xl;
    private int xr;
    private int yt;
    private int yb;
    private Color barColor;
    private Color stringColor;
    private Graphics g;
    private double stringThinness;
    private double materialHardness;
    private double lowestFrequency;
    private double loudNess;
    private double overallDecayRate;
    private double maximumForce;
    private double malletHardness;
    private double soundLength;
    private double samplingRate;
    private double pluckTime;
    private double bangBangProb;
    private double relativeScrapeDuration;
    private double max_short_volume;
    private int initial_volume;
    private AudioDataStream audioDataStream;
    private AudioPlayer audioPlayer;
    private AudioData audioData[];
    private int forceType;
    private int objectType;
    private int scrapeForceStatistics;
    private boolean isStopped;
    private Thread synthesizeThread;
    private LineSObject sob;
    private int centerx;
    private int centery;
    private int rimsize;
    private int radius;
    private Color plateColor;
    private Color cmembraneColor;
    private Color pseudoStringColor;
    Checkbox check1;
    CheckboxGroup group1;
    Checkbox check2;
    Checkbox check3;
    Checkbox check4;
    Label label1;
    Checkbox check6;
    CheckboxGroup group2;
    Checkbox check7;
    Label label2;
    Button button1;
    Button button2;
    TextField edit2;
    TextField edit3;
    Label label4;
    Label label5;
    Label label7;
    Label label8;
    TextArea edit7;
    Checkbox check8;
    Checkbox check9;
    Checkbox check10;
    Checkbox check11;
    Checkbox check5;
    TextField edit8;
    TextField edit6;
    Label label3;
    TextField edit1;
    TextField edit4;
    Label label6;
    TextField edit5;
    Label label9;
    VerticalSlider verticalSlider1;
    Label label10;

    void verticalSlider1_Action(Event event) {
        verticalSlider1.setValue(verticalSlider1.getValue());
        max_short_volume = -2166.6666666666665D * (double)verticalSlider1.getValue() + 22166.666666666668D;
        start();
    }

    public void run() {
        computeSounds();
    }

    private void computeSounds() {
        audioData = new AudioData[n_samples];
        audioPlayer = AudioPlayer.player;
        if(objectType == 0)
            sob = new LineSObject(overallDecayRate, 1.0D / materialHardness, lowestFrequency, n_samples, loudNess, max_short_volume);
        else
        if(objectType == 1)
            sob = new CPlateCSObject(overallDecayRate, 1.0D / materialHardness, lowestFrequency, n_samples, loudNess, max_short_volume);
        else
        if(objectType == 2)
            sob = new BarCCSObject(overallDecayRate, 1.0D / materialHardness, lowestFrequency, n_samples, loudNess, max_short_volume);
        else
        if(objectType == 3)
            sob = new BarCFSObject(overallDecayRate, 1.0D / materialHardness, lowestFrequency, n_samples, loudNess, max_short_volume);
        else
        if(objectType == 4)
            sob = new CMembraneSObject(overallDecayRate, 1.0D / materialHardness, lowestFrequency, n_samples, loudNess, max_short_volume);
        else
        if(objectType == 5)
            sob = new PseudoStringSObject(overallDecayRate, 1.0D / materialHardness, lowestFrequency, n_samples, loudNess, max_short_volume);
        else
            System.out.println("computeSounds(): Error in objectType");
        double ad[] = new double[2];
        if(forceType == 0) {
            BangForce bangforce = new BangForce(samplingRate, soundLength);
            bangforce.makeForce(maximumForce, 1.0D / malletHardness);
            ad = ((SonicForce) (bangforce)).force;
        } else
        if(forceType == 1) {
            if(scrapeForceStatistics == 4) {
                ScrapeForce scrapeforce = new ScrapeForce(samplingRate, soundLength);
                scrapeforce.makeForce(maximumForce, soundLength * relativeScrapeDuration);
                ad = ((SonicForce) (scrapeforce)).force;
            } else
            if(scrapeForceStatistics == 5) {
                GaussianScrapeForce gaussianscrapeforce = new GaussianScrapeForce(samplingRate, soundLength);
                gaussianscrapeforce.makeForce(maximumForce, soundLength * relativeScrapeDuration);
                ad = ((SonicForce) (gaussianscrapeforce)).force;
            } else {
                System.out.println("computeSounds(): Error in scrapeForceStatistics");
            }
        } else
        if(forceType == 2) {
            PluckForce pluckforce = new PluckForce(samplingRate, soundLength);
            pluckforce.makeForce(maximumForce, pluckTime);
            ad = ((SonicForce) (pluckforce)).force;
        } else
        if(forceType == 3) {
            Bang2Force bang2force = new Bang2Force(samplingRate, soundLength);
            bang2force.makeForce(maximumForce, 1.0D / malletHardness);
            ad = ((SonicForce) (bang2force)).force;
        } else {
            System.out.println("computeSounds(): Error in forceType");
        }
        for(int i = 0; i < n_samples; i++) {
            double d1 = sob.centerOfSound(i);
            byte abyte0[] = sob.writeSound(samplingRate, ad, d1, soundLength, 0.0D);
            audioData[i] = new AudioData(abyte0);
            double d = (100D * (double)(i + 1)) / (double)n_samples;
            edit7.setText("");
            String s = "Computed " + d + "%\n";
            edit7.appendText(s);
        }

        edit7.setText("");
        edit7.appendText("Ready\n");
    }

    private void paintString(Graphics g1) {
        g1.setColor(stringColor);
        g1.fill3DRect(xl, yt, xr - xl, (int)((double)(yb - yt) * stringThinness), true);
        g1.setColor(Color.gray);
        int i = yb - yt;
        i = i;
        g1.fill3DRect(xl - 3 * i, yt - i, 3 * i, 3 * i, true);
        g1.fill3DRect(xr, yt - i, 3 * i, 3 * i, true);
    }

    private void paintPseudoString(Graphics g1) {
        g1.setColor(pseudoStringColor);
        g1.fill3DRect(xl, yt, xr - xl, (int)((double)(yb - yt) * stringThinness), true);
        g1.setColor(Color.gray);
        int i = yb - yt;
        i = i;
        g1.fill3DRect(xl - 3 * i, yt - i, 3 * i, 3 * i, true);
        g1.fill3DRect(xr, yt - i, 3 * i, 3 * i, true);
    }

    private void paintBarCC(Graphics g1) {
        g1.setColor(barColor);
        g1.fill3DRect(xl, yt, xr - xl, yb - yt, true);
        g1.setColor(Color.gray);
        int i = yb - yt;
        i = i;
        g1.fill3DRect(xl - 3 * i, yt - i, 3 * i, 3 * i, true);
        g1.fill3DRect(xr, yt - i, 3 * i, 3 * i, true);
    }

    private void paintBarCF(Graphics g1) {
        g1.setColor(barColor);
        g1.fill3DRect(xl, yt, xr - xl, yb - yt, true);
        g1.setColor(Color.gray);
        int i = yb - yt;
        i = i;
        g1.fill3DRect(xl - 3 * i, yt - i, 3 * i, 3 * i, true);
    }

    private void paintCPlate(Graphics g1) {
        int i = radius;
        int j = radius - rimsize;
        int i1 = 2 * i;
        int j1 = 2 * i;
        int k = centerx - i;
        int l = centery - i;
        g1.setColor(Color.gray);
        g1.fillOval(k, l, i1, j1);
        int i2 = 2 * j;
        int j2 = 2 * j;
        int k1 = centerx - j;
        int l1 = centery - j;
        g1.setColor(plateColor);
        g1.fillOval(k1, l1, i2, j2);
        g1.setColor(Color.yellow);
        g1.drawOval(k1, l1, i2, j2);
        g1.setColor(Color.white);
        g1.drawOval(k, l, i1, j1);
    }

    private void paintCMembrane(Graphics g1) {
        int i = radius;
        int j = radius - rimsize;
        int i1 = 2 * i;
        int j1 = 2 * i;
        int k = centerx - i;
        int l = centery - i;
        g1.setColor(Color.gray);
        g1.fillOval(k, l, i1, j1);
        int i2 = 2 * j;
        int j2 = 2 * j;
        int k1 = centerx - j;
        int l1 = centery - j;
        g1.setColor(cmembraneColor);
        g1.fillOval(k1, l1, i2, j2);
        g1.setColor(Color.yellow);
        g1.drawOval(k1, l1, i2, j2);
        g1.setColor(Color.white);
        g1.drawOval(k, l, i1, j1);
    }

    public void paint(Graphics g1) {
        if(objectType == 0) {
            paintString(g1);
            return;
        }
        if(objectType == 1) {
            paintCPlate(g1);
            return;
        }
        if(objectType == 2) {
            paintBarCC(g1);
            return;
        }
        if(objectType == 3) {
            paintBarCF(g1);
            return;
        }
        if(objectType == 4) {
            paintCMembrane(g1);
            return;
        }
        if(objectType == 5)
            paintPseudoString(g1);
    }

    private void readParameters() {
        String s = getParameter("left");
        String s1 = getParameter("right");
        String s2 = getParameter("top");
        String s3 = getParameter("bottom");
        String s4 = getParameter("nsounds");
        String s5 = getParameter("stringcolor");
        getParameter("materialHardness");
        getParameter("volume");
        try {
            materialHardness = Double.valueOf(getParameter("materialHardness")).doubleValue();
            malletHardness = Double.valueOf(getParameter("malletHardness")).doubleValue();
            soundLength = Double.valueOf(getParameter("soundLength")).doubleValue();
            stringThinness = Double.valueOf(getParameter("stringThinness")).doubleValue();
            lowestFrequency = Double.valueOf(getParameter("lowestFrequency")).doubleValue();
            pluckTime = Double.valueOf(getParameter("pluckTime")).doubleValue();
            relativeScrapeDuration = Double.valueOf(getParameter("relativeScrapeDuration")).doubleValue();
            initial_volume = (int)Double.valueOf(getParameter("volume")).doubleValue();
            max_short_volume = 22166.666666666668D + -2166.6666666666665D * (double)initial_volume;
            String s6 = getParameter("forceType");
            if(s6.equals("bang"))
                forceType = 0;
            else
            if(s6.equals("scrape"))
                forceType = 1;
            else
            if(s6.equals("pluck"))
                forceType = 2;
            else
            if(s6.equals("bang2")) {
                forceType = 3;
            } else {
                System.out.println("forceType = bang | scrape | pluck | bang2 ");
                System.exit(1);
            }
            String s8 = getParameter("scrapeForceStatistics");
            if(s8.equals("whiteNoise"))
                scrapeForceStatistics = 4;
            else
            if(s8.equals("gaussianNoise")) {
                scrapeForceStatistics = 5;
            } else {
                System.out.println("scrapeForceStatistics  = whiteNoise | gaussianNoise ");
                System.exit(1);
            }
            String s10 = getParameter("objectType");
            if(s10.equals("cplatec"))
                objectType = 1;
            else
            if(s10.equals("string"))
                objectType = 0;
            else
            if(s10.equals("barcc"))
                objectType = 2;
            else
            if(s10.equals("barcf"))
                objectType = 3;
            else
            if(s10.equals("cmembrane")) {
                objectType = 4;
            } else {
                System.out.println("objectType = string | cplatec | barcc | barcf | cmembrane");
                System.exit(1);
            }
            xl = Integer.parseInt(s);
            xr = Integer.parseInt(s1);
            yt = Integer.parseInt(s2);
            yb = Integer.parseInt(s3);
            n_samples = Integer.parseInt(s4);
            int i = Integer.parseInt(s5, 16);
            stringColor = new Color(i);
        }
        catch(NumberFormatException _ex) {
            System.out.println("Error1 in applet parameters");
            System.exit(1);
        }
        String s7 = getParameter("centerx");
        String s9 = getParameter("centery");
        String s11 = getParameter("rimsize");
        String s12 = getParameter("radius");
        String s13 = getParameter("platecolor");
        try {
            centerx = Integer.parseInt(s7);
            centery = Integer.parseInt(s9);
            rimsize = Integer.parseInt(s11);
            radius = Integer.parseInt(s12);
            int j = Integer.parseInt(s13, 16);
            plateColor = new Color(j);
        }
        catch(NumberFormatException _ex) {
            System.out.println("Error2 in applet parameters");
            System.exit(1);
        }
        String s14 = getParameter("barcolor");
        try {
            int k = Integer.parseInt(s14, 16);
            barColor = new Color(k);
        }
        catch(NumberFormatException _ex) {
            System.out.println("Error3 in applet parameters");
            System.exit(1);
        }
        String s15 = getParameter("cmembranecolor");
        try {
            int l = Integer.parseInt(s15, 16);
            cmembraneColor = new Color(l);
        }
        catch(NumberFormatException _ex) {
            System.out.println("Error4 in applet parameters");
            System.exit(1);
        }
        String s16 = getParameter("pseudostringcolor");
        try {
            int i1 = Integer.parseInt(s16, 16);
            pseudoStringColor = new Color(i1);
            return;
        }
        catch(NumberFormatException _ex) {
            System.out.println("Error5 in applet parameters");
        }
        System.exit(1);
    }

    private void getUIvalues() {
        double d = 200D;
        int i = 1;
        try {
            d = materialHardness;
            materialHardness = Double.valueOf(edit1.getText()).doubleValue();
            if(materialHardness < 1.0D || materialHardness > 10000D)
                throw new NumberFormatException("material error");
        }
        catch(NumberFormatException _ex) {
            String s = "Hardness must be " + 1.0D + " - " + 10000D + "\n";
            edit7.appendText(s);
            materialHardness = d;
            edit1.setText("" + d);
        }
        try {
            d = lowestFrequency;
            lowestFrequency = Double.valueOf(edit2.getText()).doubleValue();
            if(lowestFrequency < 1.0D || lowestFrequency > 1000D)
                throw new NumberFormatException("Frequency ");
        }
        catch(NumberFormatException _ex) {
            String s1 = "Frequency must be " + 1.0D + " - " + 1000D + "\n";
            edit7.appendText(s1);
            lowestFrequency = d;
            edit2.setText("" + d);
        }
        try {
            i = n_samples;
            n_samples = Integer.valueOf(edit3.getText()).intValue();
            if(n_samples < 1 || n_samples > 10000)
                throw new NumberFormatException(" error");
        }
        catch(NumberFormatException _ex) {
            String s2 = "Grid size must be " + 1 + " - " + 10000 + "\n";
            edit7.appendText(s2);
            n_samples = i;
            edit3.setText("" + i);
        }
        try {
            d = soundLength;
            soundLength = Double.valueOf(edit4.getText()).doubleValue();
            if(soundLength < 0.0001D || soundLength > 60D)
                throw new NumberFormatException("SoundLength ");
        }
        catch(NumberFormatException _ex) {
            String s3 = "Duration must be " + 0.0001D + " - " + 60D + "\n";
            edit7.appendText(s3);
            soundLength = d;
            edit4.setText("" + d);
        }
        try {
            d = relativeScrapeDuration;
            relativeScrapeDuration = Double.valueOf(edit5.getText()).doubleValue();
            if(relativeScrapeDuration < 0.01D || relativeScrapeDuration > 1.0D)
                throw new NumberFormatException("relativeScrapeDuration");
        }
        catch(NumberFormatException _ex) {
            String s4 = "Relative scrape duration must be " + 0.01D + " - " + 1.0D + "\n";
            edit7.appendText(s4);
            relativeScrapeDuration = d;
            edit5.setText("" + d);
        }
        try {
            d = pluckTime;
            double d1 = soundLength;
            pluckTime = Double.valueOf(edit6.getText()).doubleValue();
            if(pluckTime < 0.0001D || pluckTime > d1)
                throw new NumberFormatException("pluckTime");
        }
        catch(NumberFormatException _ex) {
            String s5 = "Absolute pluck duration must be " + 0.0001D + " - " + "sound lenght\n";
            edit7.appendText(s5);
            pluckTime = d;
            edit6.setText("" + d);
        }
        try {
            d = malletHardness;
            malletHardness = Double.valueOf(edit8.getText()).doubleValue();
            if(malletHardness < 10D || malletHardness > 100000D)
                throw new NumberFormatException("malletHardness");
        }
        catch(NumberFormatException _ex) {
            String s6 = "Hammer hardness must be " + 10D + " - " + 100000D + "\n";
            edit7.appendText(s6);
            malletHardness = d;
            edit8.setText("" + d);
        }
    }

    public void stop() {
        isStopped = true;
        if(synthesizeThread != null && synthesizeThread.isAlive()) {
            synthesizeThread.stop();
            synthesizeThread = null;
        }
        edit7.setText("");
        edit7.appendText("Stopped\n");
        edit7.appendText("Set options and press Build\n");
    }

    public void start() {
        isStopped = false;
        repaint();
        getUIvalues();
        if(synthesizeThread != null && synthesizeThread.isAlive())
            synthesizeThread.stop();
        synthesizeThread = new Thread(this);
        synthesizeThread.start();
        edit7.appendText("Running... (press Stop to end)\n");
    }

    public void init() {
        g = getGraphics();
        readParameters();
        setLayout(null);
        addNotify();
        resize(715, 535);
        setBackground(new Color(0xc0c0c0));
        group1 = new CheckboxGroup();
        check1 = new Checkbox("Hit 1", group1, false);
        check1.reshape(24, 192, 144, 24);
        add(check1);
        check2 = new Checkbox("Pluck", group1, false);
        check2.reshape(24, 240, 144, 24);
        add(check2);
        check3 = new Checkbox("Scrape (white noise)", group1, false);
        check3.reshape(24, 264, 180, 24);
        add(check3);
        check4 = new Checkbox("Scrape (Gaussian noise)", group1, true);
        check4.reshape(24, 288, 198, 24);
        add(check4);
        label1 = new Label("Force");
        label1.reshape(36, 144, 90, 32);
        label1.setFont(new Font("Courier", 1, 20));
        add(label1);
        group2 = new CheckboxGroup();
        check6 = new Checkbox("Bar 1", group2, false);
        check6.reshape(228, 216, 108, 24);
        add(check6);
        check7 = new Checkbox("Circular Plate", group2, false);
        check7.reshape(228, 264, 120, 24);
        add(check7);
        label2 = new Label("Object");
        label2.reshape(240, 144, 90, 32);
        label2.setFont(new Font("Courier", 1, 20));
        add(label2);
        button1 = new Button("Build");
        button1.reshape(360, 168, 54, 40);
        add(button1);
        button2 = new Button("Stop");
        button2.reshape(360, 216, 54, 40);
        add(button2);
        edit2 = new TextField(10);
        edit2.reshape(576, 192, 108, 32);
        add(edit2);
        edit3 = new TextField(10);
        edit3.reshape(576, 228, 108, 32);
        add(edit3);
        label4 = new Label("Base freq.");
        label4.reshape(432, 192, 90, 24);
        add(label4);
        label5 = new Label("Grid size");
        label5.reshape(432, 228, 90, 24);
        add(label5);
        label7 = new Label("Scrape time");
        label7.reshape(432, 306, 108, 24);
        add(label7);
        label8 = new Label("Pluck time");
        label8.reshape(432, 342, 86, 24);
        add(label8);
        edit7 = new TextArea(4, 32);
        edit7.reshape(24, 342, 324, 88);
        edit7.setBackground(new Color(0xffffff));
        add(edit7);
        check8 = new Checkbox("Bar 2", group2, false);
        check8.reshape(228, 240, 108, 24);
        add(check8);
        check9 = new Checkbox("Circular Drum", group2, false);
        check9.reshape(228, 288, 144, 24);
        add(check9);
        check10 = new Checkbox("Hit 2", group1, false);
        check10.reshape(24, 216, 144, 24);
        add(check10);
        check11 = new Checkbox("Pseudo String", group2, false);
        check11.reshape(228, 312, 144, 24);
        add(check11);
        check5 = new Checkbox("String", group2, true);
        check5.reshape(228, 192, 120, 24);
        add(check5);
        edit8 = new TextField(10);
        edit8.reshape(576, 372, 108, 32);
        add(edit8);
        edit6 = new TextField(10);
        edit6.reshape(576, 336, 108, 32);
        add(edit6);
        label3 = new Label("Hardness");
        label3.reshape(432, 156, 90, 24);
        add(label3);
        edit1 = new TextField(10);
        edit1.reshape(576, 156, 108, 32);
        add(edit1);
        edit4 = new TextField(10);
        edit4.reshape(576, 264, 108, 32);
        add(edit4);
        label6 = new Label("Duration");
        label6.reshape(432, 264, 90, 24);
        add(label6);
        edit5 = new TextField(10);
        edit5.reshape(576, 300, 108, 32);
        add(edit5);
        label9 = new Label("Hammer hardness");
        label9.reshape(432, 378, 141, 24);
        add(label9);
        verticalSlider1 = new VerticalSlider();
        verticalSlider1.reshape(372, 270, 48, 138);
        add(verticalSlider1);
        label10 = new Label("Volume");
        label10.reshape(372, 414, 60, 24);
        add(label10);
        verticalSlider1.setValue(11 - initial_volume);
        edit1.setText("" + materialHardness);
        edit2.setText("" + lowestFrequency);
        edit3.setText("" + n_samples);
        edit4.setText("" + soundLength);
        edit5.setText("" + relativeScrapeDuration);
        edit6.setText("" + pluckTime);
        edit8.setText("" + malletHardness);
    }

    public void destroy() {
    }

    private protected void uimess(String s) {
        edit7.appendText(s);
    }

    private protected void clearUimess() {
        edit7.setText("");
    }

    private boolean mouseDownBarCC(Event event, int i, int j) {
        return mouseDownString(event, i, j);
    }

    private boolean mouseDownPseudoString(Event event, int i, int j) {
        return mouseDownString(event, i, j);
    }

    private boolean mouseDownBarCF(Event event, int i, int j) {
        if(j < yb && j > yt && i < xr && i > xl) {
            g.setXORMode(Color.white);
            g.fillOval(i - 2, j - 2, 5, 5);
            int k = i - xl;
            int l = xr - xl;
            int i1 = (int)(((double)k / (double)l) * (double)n_samples);
            if(i1 >= n_samples)
                i1 = n_samples - 1;
            if(!isStopped && (synthesizeThread == null || !synthesizeThread.isAlive()))
                playSound(i1);
            g.fillOval(i - 2, j - 2, 5, 5);
        }
        return true;
    }

    private boolean mouseDownString(Event event, int i, int j) {
        if(j < yb && j > yt && i < xr && i > xl) {
            g.setXORMode(Color.white);
            g.fillOval(i - 2, j - 2, 5, 5);
            int k = (xl + xr) / 2;
            int l;
            if(i <= k)
                l = i - xl;
            else
                l = xr - i;
            int i1 = (xr - xl) / 2;
            int j1 = (int)(((double)l / (double)i1) * (double)n_samples);
            if(j1 >= n_samples)
                j1 = n_samples - 1;
            if(!isStopped && (synthesizeThread == null || !synthesizeThread.isAlive()))
                playSound(j1);
            g.fillOval(i - 2, j - 2, 5, 5);
        }
        return true;
    }

    private boolean mouseDownCMembrane(Event event, int i, int j) {
        return mouseDownCPlate(event, i, j);
    }

    private boolean mouseDownCPlate(Event event, int i, int j) {
        int k = (i - centerx) * (i - centerx) + (j - centery) * (j - centery);
        int l = (radius - rimsize) * (radius - rimsize);
        if(k <= l) {
            g.setXORMode(Color.white);
            g.fillOval(i - 2, j - 2, 5, 5);
            double d = Math.sqrt((double)k / (double)l);
            int i1 = (int)(d * (double)n_samples);
            if(i1 >= n_samples)
                i1 = n_samples - 1;
            if(!isStopped && (synthesizeThread == null || !synthesizeThread.isAlive()))
                playSound(i1);
            g.fillOval(i - 2, j - 2, 5, 5);
        }
        return true;
    }

    public boolean mouseDown(Event event, int i, int j) {
        if(objectType == 0)
            return mouseDownString(event, i, j);
        if(objectType == 1)
            return mouseDownCPlate(event, i, j);
        if(objectType == 2)
            return mouseDownString(event, i, j);
        if(objectType == 3)
            return mouseDownBarCF(event, i, j);
        if(objectType == 4)
            return mouseDownCPlate(event, i, j);
        if(objectType == 5)
            return mouseDownString(event, i, j);
        else
            return false;
    }

    private void playSound(int i) {
        AudioDataStream audiodatastream = new AudioDataStream(audioData[i]);
        if(!isStopped) {
            audioPlayer.start(audiodatastream);
            return;
        } else {
            System.out.println("Stopped");
            return;
        }
    }

    public void clickedCheck1() {
        forceType = 0;
        start();
    }

    public void clickedCheck10() {
        forceType = 3;
        start();
    }

    public void clickedCheck2() {
        forceType = 2;
        start();
    }

    public void clickedCheck3() {
        scrapeForceStatistics = 4;
        forceType = 1;
        start();
    }

    public void clickedCheck4() {
        scrapeForceStatistics = 5;
        forceType = 1;
        start();
    }

    public void clickedCheck5() {
        objectType = 0;
        start();
    }

    public void clickedCheck7() {
        objectType = 1;
        start();
    }

    public void clickedCheck6() {
        objectType = 2;
        start();
    }

    public void clickedCheck8() {
        objectType = 3;
        start();
    }

    public void clickedCheck9() {
        objectType = 4;
        start();
    }

    public void clickedCheck11() {
        objectType = 5;
        start();
    }

    public void clickedCheck12() {
        objectType = 6;
        start();
    }

    public void clickedButton1() {
        start();
    }

    public void clickedButton2() {
        stop();
    }

    public boolean handleEvent(Event event) {
        if(event.id == 1001 && event.target == check11) {
            clickedCheck11();
            return true;
        }
        if(event.id == 1001 && event.target == check10) {
            clickedCheck10();
            return true;
        }
        if(event.id == 1004 && event.target == edit8) {
            gotFocusEdit8(event);
            return true;
        }
        if(event.id == 1004 && event.target == edit6) {
            gotFocusEdit6(event);
            return true;
        }
        if(event.id == 1004 && event.target == edit5) {
            gotFocusEdit5(event);
            return true;
        }
        if(event.id == 1004 && event.target == edit4) {
            gotFocusEdit4(event);
            return true;
        }
        if(event.id == 1004 && event.target == edit3) {
            gotFocusEdit3(event);
            return true;
        }
        if(event.id == 1004 && event.target == edit2) {
            gotFocusEdit2(event);
            return true;
        }
        if(event.id == 1001 && event.target == check9) {
            clickedCheck9();
            return true;
        }
        if(event.id == 1001 && event.target == check8) {
            clickedCheck8();
            return true;
        }
        if(event.id == 1001 && event.target == check6) {
            clickedCheck6();
            return true;
        }
        if(event.id == 1001 && event.target == check7) {
            clickedCheck7();
            return true;
        }
        if(event.id == 1004 && event.target == edit1) {
            gotFocusEdit1(event);
            return true;
        }
        if(event.id == 1001 && event.target == button2) {
            clickedButton2();
            return true;
        }
        if(event.id == 1001 && event.target == button1) {
            clickedButton1();
            return true;
        }
        if(event.id == 1001 && event.target == check5) {
            clickedCheck5();
            return true;
        }
        if(event.id == 1001 && event.target == check4) {
            clickedCheck4();
            return true;
        }
        if(event.id == 1001 && event.target == check3) {
            clickedCheck3();
            return true;
        }
        if(event.id == 1001 && event.target == check2) {
            clickedCheck2();
            return true;
        }
        if(event.id == 1001 && event.target == check1) {
            clickedCheck1();
            return true;
        }
        if(event.target == verticalSlider1 && event.id == 1001) {
            verticalSlider1_Action(event);
            return true;
        } else {
            return super.handleEvent(event);
        }
    }

    public void gotFocusEdit1(Event event) {
        edit7.setText("");
        String s = 1.0D + " - " + 10000D + ". Represents material hardness for\n";
        edit7.appendText(s);
        edit7.appendText("solids, represents damping behaviour for strings\n");
        edit7.appendText("and membranes.\n");
    }

    public void gotFocusEdit2(Event event) {
        edit7.setText("");
        String s = 1.0D + " - " + 1000D + ". Frequency of lowest partial.\n";
        edit7.appendText(s);
        edit7.appendText("Lower sounds need more computation.\n");
    }

    public void gotFocusEdit3(Event event) {
        edit7.setText("");
        String s = 1 + " - " + 10000 + ". Number of different\n";
        edit7.appendText(s);
        edit7.appendText("sounds mapped onto object.\n");
    }

    public void gotFocusEdit4(Event event) {
        edit7.setText("");
        String s = 0.0001D + " - " + 60D + ". Length in seconds\n";
        edit7.appendText(s);
        edit7.appendText("of sound response to mouse click.\n");
    }

    public void gotFocusEdit5(Event event) {
        edit7.setText("");
        String s = 0.01D + " - " + 1.0D + ". For scraping force. Fraction of\n";
        edit7.appendText(s);
        edit7.appendText("the sample time that the objects is scraped.\n");
        edit7.appendText("The rest of the time it is ringing.\n");
    }

    public void gotFocusEdit6(Event event) {
        edit7.setText("");
        String s = 0.0001D + " - " + soundLength + ". For plucking force. Length of\n";
        edit7.appendText(s);
        edit7.appendText("time (in seconds) that the objects is pulled\n");
        edit7.appendText("before release.\n");
    }

    public void gotFocusEdit8(Event event) {
        edit7.setText("");
        String s = 10D + " - " + 100000D + ". For hitting force. Reciprocal of\n";
        edit7.appendText(s);
        edit7.appendText("the length of time (in seconds) that the object\n");
        edit7.appendText("is in contact with the hammer.\n");
    }

    public SoundObjectsApplet() {
        stringThinness = 1.0D;
        materialHardness = 100D;
        lowestFrequency = 415D;
        loudNess = 1.0D;
        maximumForce = 1.0D;
        malletHardness = 10000D;
        soundLength = 1.0D;
        samplingRate = 8012D;
        pluckTime = 0.30000000000000004D;
        bangBangProb = 0.01D;
        relativeScrapeDuration = 0.5D;
        max_short_volume = 1000D;
        scrapeForceStatistics = 4;
        isStopped = false;
    }
}
